package com.niit.bc.servlet;

import com.niit.bc.bean.BookBean;
import com.niit.bc.bean.ListBean;
import com.niit.bc.biz.BookBiz;
import com.niit.bc.biz.ListBiz;
import com.niit.bc.biz.impl.BookBizImpl;
import com.niit.bc.biz.impl.ListBizImpl;
import com.niit.bc.util.web.Constants;
import com.niit.bc.util.web.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SearchBook extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("utf8");
        request.setCharacterEncoding("utf8");

        /**
         * 左边图书分类列表
         * 分为大类型列表和小类型列表，其实现方式为找出大类型的id和name，通过大类型的id在小类型的表中查找
         * 相应的小类型名称，关键代码为<c:if test="${listType.typeId == listChildType.typeId }">
         */

        //列出所有类型的列表

        ListBiz listTypeBiz = new ListBizImpl();
        List <ListBean> listType = listTypeBiz.findListType();
        if (listType != null) {
            request.setAttribute("listType", listType);
        }

        //列出所有子类型的列表

        ListBiz listChildTypeBiz = new ListBizImpl();
        List <ListBean> listChildType = listChildTypeBiz.findListChildType();
        if (listChildType != null) {
            request.setAttribute("listChildType", listChildType);
        }

        /**
         * 左边图书的各种排行榜(取得bookId,bookName,bookAuthor,bookPhoto,bookPrice,bookPub,bookPubTime...)
         * 根据首页的需要来改变字段的选取，这样比全部选取book字段效率要高，此块查询语句可以在进行修改的
         * 一般为需要的图书基本信息
         */
        BookBiz bookBiz = new BookBizImpl();

        //推荐图书--点击数或者浏览量top10

        String sql1 = "SELECT bc_books.* FROM bc_books ORDER BY bookHits DESC LIMIT 0, 10;";
        List <BookBean> bookHitsList = bookBiz.findBookList(sql1);
        if (bookHitsList != null) {
            request.setAttribute("bookHitsList", bookHitsList);
        }

        //图书销售排行榜 --销售排行榜top10

        String sql2 = "SELECT bc_books.* FROM bc_books ORDER BY bookSale DESC LIMIT 0, 10;";
        List <BookBean> bookSaleList = bookBiz.findBookList(sql2);
        if (bookSaleList != null) {
            request.setAttribute("bookSaleList", bookSaleList);
        }

        //新书推荐 --上架时间top10

        String sql3 = "SELECT bc_books.* FROM bc_books ORDER BY bookStorageTime DESC LIMIT 0, 10;";
        List <BookBean> bookStorageTimeList = bookBiz.findBookList(sql3);
        if (bookStorageTimeList != null) {
            request.setAttribute("bookStorageTimeList", bookStorageTimeList);
        }


        PageUtil pageUtil = new PageUtil(Constants.BOOKLIST_PAGESIZE, 0);
        //获取分页信息
        String spageCount = request.getParameter("pageCount");
        if (spageCount != null && !spageCount.trim().equals("")) {
            pageUtil.setPageCount(Integer.parseInt(spageCount));
        }

        String srowCount = request.getParameter("rowCount");
        if (srowCount != null && !srowCount.trim().equals("")) {
            pageUtil.setRowCount(Integer.parseInt(srowCount));
        }

        String spageCurr = request.getParameter("pageCurr");
        if (spageCurr != null && !spageCurr.trim().equals("")) {
            pageUtil.setPageCurr(Integer.parseInt(spageCurr));
        }

        //根据关键字搜索图书列表

        List <BookBean> list = new ArrayList <BookBean>();
        BookBiz biz = new BookBizImpl();
        String key = request.getParameter("key");
        if (key != null && !key.trim().equals("")) {
            list = biz.findBookByKey(key, pageUtil);
            if (list != null && list.size() != 0) {
                request.setAttribute("list", list);
                request.setAttribute("pageUtil", pageUtil);
                request.getRequestDispatcher("searchBook.jsp").forward(request, response);
            } else {
                request.setAttribute("msg", "该书城暂时没有符合您要求的图书，请搜索其它图书。");
                request.getRequestDispatcher("searchBook.jsp").forward(request, response);
            }
        }
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
